package me.khrystal.cleanmaster.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import me.khrystal.cleanmaster.dialogs.ProgressDialogFragment;
import me.khrystal.cleanmaster.utils.T;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/9/13
 * update time:
 * email: 723526676@qq.com
 */
public abstract class BaseActivity extends AppCompatActivity{

    protected int mScreenWidth;
    protected int mScreenHeight;
    protected float mDensity;
    protected Context mContext;
    protected String LogName;
    protected ImageView titleBack;
    protected TextView titleName;
    protected TextView titleRightText;

    private static String mDialogTag = "basedialog";

    ProgressDialogFragment mProgressDialogFragment;

    protected Boolean isFinish = false;
    protected ActivityTask task = ActivityTask.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        mScreenWidth = metrics.widthPixels;
        mScreenHeight = metrics.heightPixels;
        mDensity = metrics.density;
        LogName = this.getClass().getSimpleName();
        task.addActivity(this);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
    }

    protected void startActivity(Class<?> cls) {
        startActivity(cls,null);
    }

    protected void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mContext, cls);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivity(intent);
    }

    protected void startActivity(String action) {
        startActivity(action, null);
    }

    protected void startActivity(String action, Bundle bundle) {
        Intent intent = new Intent();
        intent.setAction(action);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivity(intent);
    }

    protected void showShort(String message) {
        T.showShort(mContext, message);
    }

    protected void showLong(String message) {
        T.showLong(mContext, message);
    }

    @Override
    public void finish() {
        super.finish();
        task.removeActivity(this);
    }

    public void showDialogLoading() {
        showDialogLoading(null);
    }

    public void showDialogLoading(String msg) {

        if (mProgressDialogFragment == null) {
            mProgressDialogFragment = ProgressDialogFragment.newInstance(0, null);
        }

        if (!TextUtils.isEmpty(msg))
            mProgressDialogFragment.setMessage(msg);

        mProgressDialogFragment.show(getFragmentManager(), mDialogTag);
    }

    public void dismissDialogLoading() {
        if (mProgressDialogFragment != null)
            mProgressDialogFragment.dismissAllowingStateLoss();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
